package display2012;

import it.unibo.is.interfaces.IBasicEnvAwt;

public abstract class DisplayContaKm extends Display {
	public DisplayContaKm(IBasicEnvAwt env) {
		super(env);
	}

	@Override
	public void update(String v) {
		boolean check = checkVal(v);
		if (check) {
			super.update(v);
		} else if (checkDigitVal(v)) {
			super.update(toDisplayStr(new Integer(v)));

		}

	}

	protected boolean checkDigitVal(String v) {
		for (int i = 0; i < v.length() - 1; i++) {
			if (!isDigit(v.charAt(i)))
				return false;
		}
		return true;
	}

	protected boolean checkVal(String v) {
		if (v.length() != 6)
			return false;
		for (int i = 0; i < v.length() - 1; i++) {
			if (!isDigit(v.charAt(i)))
				return false;
		}
		return true;
	}

	protected boolean isDigit(char ch) {
		return ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4'
				|| ch == '5' || ch == '6' || ch == '7' || ch == '8'
				|| ch == '9';
	}

	protected String toDisplayStr(int v) {
		String vs = "" + v;
		while (vs.length() < 6)
			vs = "0" + vs;
		return vs;
	}
}